package cn.bdqn.fin.service.impl;

import cn.bdqn.fin.mapper.ApplyMapper;
import cn.bdqn.fin.mapper.VApplyMapper;
import cn.bdqn.fin.model.Apply;
import cn.bdqn.fin.model.PaymentType;
import cn.bdqn.fin.model.VApply;
import cn.bdqn.fin.model.VApplyExample;
import cn.bdqn.fin.service.ApplyService;
import cn.bdqn.fin.vomapper.PaymentTypeVoMapper;
import cn.bdqn.model.EUList;
import cn.bdqn.model.PageBean;
import cn.bdqn.model.R;
import cn.bdqn.service.BaseService;
import cn.bdqn.veh.model.Leader;
import cn.bdqn.veh.vomapper.VehicleVoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author gurilla
 * @create 2019-08-21 21:12
 */
@Service
public class ApplyServiceImpl extends BaseService implements ApplyService {
    @Resource
    private VApplyMapper vApplyMapper;

    @Resource
    private VehicleVoMapper vehicleVoMapper;

    @Resource
    private PaymentTypeVoMapper paymentTypeVoMapper;

    @Resource
    private ApplyMapper applyMapper;

    @Override
    public EUList<VApply> list(PageBean pageBean, String lname, String phone, String account, String drawee) {
        VApplyExample example = new VApplyExample();
        VApplyExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(lname) || !StringUtils.isEmpty(account) || !StringUtils.isEmpty(phone) || !StringUtils.isEmpty(drawee)){
            criteria.andLnameLike("%"+lname+"%");
            criteria.andAccountLike("%"+account+"%");
            criteria.andPhoneLike("%"+phone+"%");
            criteria.andDraweeLike("%"+drawee+"%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        List<VApply> vApplies = vApplyMapper.selectByExample(example);
        PageInfo<VApply> info=new PageInfo<>(vApplies);
        return new EUList<>(info.getTotal(),info.getList());
    }

    @Override
    public R save(Apply apply) {
 //       VApply vApply = new VApply();
        if(apply.getId()==null){
            apply.setOpid(getUserid());
            apply.setCreateTime(new Date());
            apply.setLastTime(new Date());
            applyMapper.insertSelective(apply);
        }else{
 //           apply.setId(vApply.getId());
            apply.setOpid(getUserid());
            apply.setLastTime(new Date());
            applyMapper.updateByPrimaryKeySelective(apply);
        }
        return R.ok();
    }

    @Override
    public R getLeader() {
        List<Leader> leaders = vehicleVoMapper.getAllLeader();
        return R.ok(null, leaders);
    }

    @Override
    public R getPaymentType() {
        List<PaymentType> paymentTypes = paymentTypeVoMapper.getPaymentType();
        return R.ok(null, paymentTypes);
    }
}
